package com.zhiyuancode.mqack.util;

import java.util.Objects;

/**
 * 异常工具类
 *
 * @author 杨志远
 * email faryangsh@163.com
 * Date    2018-05-03
 */
public class ExceptionUtil {

    public static String getDetailErrorMessage(String basicErrorMessage, Throwable cause, Object... params) {
        StringBuilder message = new StringBuilder(512);

        message.append(basicErrorMessage);

        while (Objects.nonNull(cause)) {
            message.append("\n;  cause-").append(cause.getClass().getSimpleName()).append(":").append(getCauseErrorMessage(cause));
            cause = cause.getCause();
        }

        if (Objects.nonNull(params) && params.length > 0) {
            for (Object param : params) {
                message.append("\n;  param-").append(param.getClass().getSimpleName()).append(":").append(param);
            }
        }

        return message.toString();
    }

    public static String getSimpleCauseErrorMessage(Throwable cause) {
        return getCauseErrorMessage(cause, false);
    }

    private static String getCauseErrorMessage(Throwable cause) {
        return getCauseErrorMessage(cause, true);
    }

    private static String getCauseErrorMessage(Throwable cause, boolean needCauseErrorCode) {
        if (Objects.isNull(cause)) {
            return "";
        }
        return cause.getMessage();
    }
}
